package com.lingdian.member.controller;

import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSONObject;
import com.lingdian.common.vo.JSONResult;
import com.lingdian.member.bean.Member;
import com.lingdian.member.repository.MemberRepository;

@Controller
@RequestMapping("/member")
//@SessionAttributes("member")
public class MemberController {
	
	private MemberRepository memberRepository;
	
	public MemberController(MemberRepository memberRepository) {
		this.memberRepository = memberRepository;
	}
	@RequestMapping("/toLogin")
	public String add(){
		return "member/add";
				
	}
	@RequestMapping("/save")
	@ResponseBody
	public JSONResult save(@RequestBody Member member){
		memberRepository.save(member);
		return new JSONResult(200, "", "success");
	}
	@RequestMapping("/login")
	public String login(){
//		Member m = memberRepository.findByloginNameAndPassword(member.getLoginName(), member.getPassword());
	/*	if(m!=null){
			modelMap.addAttribute("member", m);
		}*/
		return "member/add";
	}
	@RequestMapping("/findByRealName")
	@ResponseBody
	public Page<Member> findByRealName(@RequestBody JSONObject params){
		Page<Member> page = memberRepository.findByRealName(params.getString("realName"), 
				new PageRequest(params.getIntValue("page"),params.getIntValue("size")));
		return page;
	}
}
